package www.reviewW.view;

import www.reviewW.data.Knowledge;
import www.reviewW.data.ModeData;
import www.reviewW.data.ReviewWData;

import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

public class Main extends JFrame implements ActionListener {
    JPanel content;
    JScrollPane jsContent = new JScrollPane();
    JButton newMode;//创建一个新的分栏
    List<JButton> deleteMode; //创建一个新的分栏
    public Main(){
        //基础窗口设置
        this.setTitle("ReviewW");
        this.setSize(800,600);
        this.setLocationRelativeTo(null);
        this.setResizable(false);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setLayout(new BorderLayout());
        this.add(new JLabel("New46 SO EASY SO HAPPY"),"North");

        refreshContent(this); //中间面板的装配




        this.setVisible(true);
    }
    public static void main(String[] args){
        new Main();
    }
    public void refreshContent(JFrame jFrame){ //刷新面板
        deleteMode = new ArrayList<>();
        content = new JPanel();
        content.setLayout(new GridLayout(1,0));

        jsContent.getViewport().add(content);

        JPanel temp; //中间转换面板 中间转换面板为四周性布局
        try{ //添加分栏
            ReviewWData reviewWData = ReviewWData.ObjectIn();
            for(ModeData modeData:reviewWData.getModeDatas()){
                temp = new JPanel();

                temp.setBorder(new LineBorder(Color.black,1,true));
                temp.setLayout(new BorderLayout()); //四周性布局

                JPanel topJPanel = new JPanel(new BorderLayout());
                topJPanel.add(new JLabel(modeData.getTitle()),"West");
                JButton addKnowledge = new JButton("添加新知识点");
                addKnowledge.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        new KnowledgeJDialog(jFrame,reviewWData,modeData);
                        refreshContent(jFrame);
                    }
                });
                topJPanel.add(addKnowledge,"East");
                temp.add(topJPanel,"North");

                //上边添加标题
                //中间知识点面板
//                JPanel jPanel = new JPanel();
//                JScrollPane js = new JScrollPane(jPanel);
//                jPanel.setLayout(new GridLayout(0,3));
//                JButton jButton = new JButton("添加知识点");
//                jButton.addActionListener(new ActionListener() {
//                    @Override
//                    public void actionPerformed(ActionEvent e) {
//                        new KnowledgeJDialog(jFrame);
//                    }
//                });
//                jPanel.add(new JLabel());
//                jPanel.add(jButton);
//                jPanel.add(new JLabel());


                JPanel jPanel = new JPanel();
                jPanel.setLayout(new GridLayout(0,3));
                JScrollPane js = new JScrollPane(jPanel);
                for(Knowledge knowledge:modeData.getKnowledges()){ //中间知识点
                    jPanel.add(new JLabel(knowledge.getTitle()));

                    //暂时占位
                    jPanel.add(new JLabel(knowledge.getCreateDate().toString()));
                    JButton deleteKnowledge = new JButton("删除");
                    deleteKnowledge.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
                            try {
                                modeData.deleteKnowledge(knowledge.getId());
                                ReviewWData.ObjectOut(reviewWData);
                                refreshContent(jFrame);
                            } catch (Exception e1) {
                                e1.printStackTrace();
                            }
                        }
                    });
                    jPanel.add(deleteKnowledge);

                }
                temp.add(js);
//                while (i++ < 120){
//                    jPanel.add(new JLabel("测试数据"));
//                }




                JPanel temp2 = new JPanel();//底层转换面板
                temp2.setLayout(new GridLayout(1,2));
                JButton jButton = new JButton("知识点复习");
                jButton.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        Knowledge knowledge = modeData.RandomKnowledge();
                        new KnowledgeFuxi(jFrame,knowledge);
                    }
                });
                temp2.add(jButton);

                JButton jButtonTemp = new JButton("删除分栏");
                jButtonTemp.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {//删除分栏
                        try {
                            reviewWData.deleteModeDatas(modeData.getId());
                            ReviewWData.ObjectOut(reviewWData);
                            refreshContent(jFrame);
                        } catch (Exception e1) {
                            e1.printStackTrace();
                        }
                    }
                });
                temp2.add(jButtonTemp);
                temp.add(temp2,"South");

                content.add(temp);
            }

            temp = new JPanel(); //创建分栏面板
            newMode = new JButton("添加新的课程");
            newMode.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    String title = JOptionPane.showInputDialog(jFrame,"请输入课程标题","添加新课程",3);
                    try {
                        ReviewWData reviewWData = ReviewWData.ObjectIn();
                        reviewWData.addModeDatas(title);
                        ReviewWData.ObjectOut(reviewWData);
                        refreshContent(jFrame);
                    } catch (Exception e1) {
                        e1.printStackTrace();
                    }
                }
            });
            temp.add(newMode);
            temp.setLayout(new GridLayout(1,2));
            content.add(temp);

            this.add(jsContent);
        }catch (Exception e){

        }
    }

    //事件监视器
    @Override
    public void actionPerformed(ActionEvent e) {

    }
}
